package Vista;

import DAO.AccesoBaseProliferacion;
import Modelo.ShellAbstracto;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.table.DefaultTableModel;

public class ShellSQLITE extends javax.swing.JFrame implements ShellAbstracto
{

    /**
     * Creates new form ShellSQLITE
     */
    public ShellSQLITE() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        txtSentencia = new javax.swing.JTextArea();
        lblSentencia = new javax.swing.JLabel();
        lblResultado = new javax.swing.JLabel();
        btnSentencia = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblResultado = new javax.swing.JTable();
        lblError = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        txtError = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        txtSentencia.setColumns(20);
        txtSentencia.setRows(5);
        jScrollPane1.setViewportView(txtSentencia);

        lblSentencia.setText("Sentencia");

        lblResultado.setText("Resultado");

        btnSentencia.setText("Consultar");
        btnSentencia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSentenciaActionPerformed(evt);
            }
        });

        tblResultado.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(tblResultado);

        lblError.setText("Error");

        txtError.setColumns(20);
        txtError.setRows(5);
        jScrollPane3.setViewportView(txtError);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1)
                        .addGap(18, 18, 18)
                        .addComponent(btnSentencia, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblError)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane3))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblSentencia)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 853, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblResultado))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(lblSentencia)
                .addGap(4, 4, 4)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblError)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblResultado)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 267, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(btnSentencia))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSentenciaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSentenciaActionPerformed
        mostrarShell();
    }//GEN-LAST:event_btnSentenciaActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSentencia;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JLabel lblError;
    private javax.swing.JLabel lblResultado;
    private javax.swing.JLabel lblSentencia;
    private javax.swing.JTable tblResultado;
    private javax.swing.JTextArea txtError;
    private javax.swing.JTextArea txtSentencia;
    // End of variables declaration//GEN-END:variables

    public JButton getBtnSentencia() {
        return btnSentencia;
    }

    public void setBtnSentencia(JButton btnSentencia) {
        this.btnSentencia = btnSentencia;
    }

    public JScrollPane getjScrollPane1() {
        return jScrollPane1;
    }

    public void setjScrollPane1(JScrollPane jScrollPane1) {
        this.jScrollPane1 = jScrollPane1;
    }

    public JScrollPane getjScrollPane2() {
        return jScrollPane2;
    }

    public void setjScrollPane2(JScrollPane jScrollPane2) {
        this.jScrollPane2 = jScrollPane2;
    }

    public JScrollPane getjScrollPane3() {
        return jScrollPane3;
    }

    public void setjScrollPane3(JScrollPane jScrollPane3) {
        this.jScrollPane3 = jScrollPane3;
    }

    public JLabel getLblError() {
        return lblError;
    }

    public void setLblError(JLabel lblError) {
        this.lblError = lblError;
    }

    public JLabel getLblResultado() {
        return lblResultado;
    }

    public void setLblResultado(JLabel lblResultado) {
        this.lblResultado = lblResultado;
    }

    public JLabel getLblSentencia() {
        return lblSentencia;
    }

    public void setLblSentencia(JLabel lblSentencia) {
        this.lblSentencia = lblSentencia;
    }

    public JTable getTblResultado() {
        return tblResultado;
    }

    public void setTblResultado(JTable tblResultado) {
        this.tblResultado = tblResultado;
    }

    public JTextArea getTxtError() {
        return txtError;
    }

    public void setTxtError(JTextArea txtError) {
        this.txtError = txtError;
    }

    public JTextArea getTxtSentencia() {
        return txtSentencia;
    }

    public void setTxtSentencia(JTextArea txtSentencia) {
        this.txtSentencia = txtSentencia;
    }

    @Override
    public void mostrarShell() {
        DefaultTableModel modelo = null;
        SQLException exception = null;
        try {
            Object resultado = AccesoBaseProliferacion.getAccesoDatos().ejecutar(getTxtSentencia().getText());
            try {
                modelo = DefaultTableModel.class.cast(resultado);
            } catch (Exception e) {
                throw (Exception) resultado;
            }
            getTblResultado().setModel(modelo);
            getTxtError().setText((exception != null ? exception.getMessage() : "Sentencia SQL ejecutada sin inconvenientes."));
        } catch (Exception e) {
            getTxtError().setText((e != null ? e.getMessage() : "No hay errores"));
        }

    }
}
